package com.ltkj.ticket.service.impl;

import java.util.List;

import com.ltkj.common.core.exception.ServiceException;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.redis.service.RedisService;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.ticket.domain.TicScenic;
import com.ltkj.ticket.domain.TicScenicType;
import com.ltkj.ticket.domain.TicType;
import com.ltkj.ticket.domain.dto.TicScenicQueryDTO;
import com.ltkj.ticket.domain.dto.TicScenicStockAddDTO;
import com.ltkj.ticket.domain.dto.TicScenicUpdateDTO;
import com.ltkj.ticket.domain.vo.*;
import com.ltkj.ticket.mapper.TicScenicMapper;
import com.ltkj.ticket.mapper.TicScenicTypeMapper;
import com.ltkj.ticket.mapper.TicTypeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.stereotype.Service;
import com.ltkj.ticket.mapper.TicScenicStockMapper;
import com.ltkj.ticket.domain.TicScenicStock;
import com.ltkj.ticket.service.ITicScenicStockService;

import javax.sql.rowset.serial.SerialException;

/**
 * 景区门票库存Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-31
 */
@Service
public class TicScenicStockServiceImpl implements ITicScenicStockService 
{
    private static final Logger log = LoggerFactory.getLogger(TicScenicStockServiceImpl.class);

    @Autowired
    private TicScenicStockMapper ticScenicStockMapper;
    @Autowired
    private TicScenicMapper ticScenicMapper;
    @Autowired
    private TicScenicTypeMapper ticScenicTypeMapper;
    @Autowired
    private TicTypeMapper ticTypeMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 查询景区门票库存
     * 
     * @param scenicStockId 景区门票库存主键
     * @return 景区门票库存
     */
    @Override
    public TicScenicStock selectTicScenicStockByScenicStockId(Long scenicStockId)
    {
        return ticScenicStockMapper.selectTicScenicStockByScenicStockId(scenicStockId);
    }

    /**
     * 查询景区门票库存列表
     * 
     * @param ticScenicStock 景区门票库存
     * @return 景区门票库存
     */
    @Override
    public List<TicScenicStock> selectTicScenicStockList(TicScenicStock ticScenicStock)
    {
        return ticScenicStockMapper.selectTicScenicStockList(ticScenicStock);
    }

    /**
     * 新增景区门票库存
     * 
     * @param ticScenicStock 景区门票库存
     * @return 结果
     */
    @Override
    public int insertTicScenicStock(TicScenicStock ticScenicStock)
    {
        ticScenicStock.setCreateTime(DateUtils.getNowDate());
        return ticScenicStockMapper.insertTicScenicStock(ticScenicStock);
    }

    /**
     * 修改景区门票库存
     * 
     * @param ticScenicStock 景区门票库存
     * @return 结果
     */
    @Override
    public int updateTicScenicStock(TicScenicStock ticScenicStock)
    {
        ticScenicStock.setUpdateTime(DateUtils.getNowDate());
        return ticScenicStockMapper.updateTicScenicStock(ticScenicStock);
    }

    /**
     * 批量删除景区门票库存
     * 
     * @param scenicStockIds 需要删除的景区门票库存主键
     * @return 结果
     */
    @Override
    public int deleteTicScenicStockByScenicStockIds(Long[] scenicStockIds)
    {
        if (scenicStockIds!=null&&scenicStockIds.length>0){
            for (int i = 0; i < scenicStockIds.length; i++) {
                TicScenicStock ticScenicStock = ticScenicStockMapper.selectTicScenicStockByScenicStockId(scenicStockIds[i]);
                ticScenicTypeMapper.deleteTicScenicTypeByDate(ticScenicStock.getScenicId(),ticScenicStock.getScenicStockDate());
            }
        }
        return ticScenicStockMapper.deleteTicScenicStockByScenicStockIds(scenicStockIds);
    }

    /**
     * 删除景区门票库存信息
     * 
     * @param scenicStockId 景区门票库存主键
     * @return 结果
     */
    @Override
    public int deleteTicScenicStockByScenicStockId(Long scenicStockId)
    {
        return ticScenicStockMapper.deleteTicScenicStockByScenicStockId(scenicStockId);
    }


    /**
     * 新增景区门票库存
     *
     * @param addDTO 景区门票库存
     * @return 结果
     */
    @Override
    public int insertTicScenicStockNew(TicScenicStockAddDTO addDTO) {
        try{
            // 返回的日期集合
            List<String> days = DateUtils.getAscDateList(addDTO.getSecnicTimeStart(),addDTO.getSecnicTimeEnd());
            if (days!=null&&days.size()>0){
                TicScenic ticScenic = ticScenicMapper.selectTicScenicByScenicId(addDTO.getTicScenicId());
                List<TicScenic> list = ticScenicStockMapper.selectTicScenicListByDays(addDTO.getTicScenicId(),days);
                if (list!=null&&list.size()>0){
                    throw new ServiceException("门票日期库存已存在，请勿重复添加!");
                }
                //批量插入日期库存数据
                ticScenicStockMapper.insertBatch(days,addDTO.getScenicStockCount(),addDTO.getTicScenicId(),ticScenic.getScenicName(), SecurityUtils.getUsername());
                for (int i = 0; i < days.size(); i++) {
                    if (addDTO.getTicScenicTypeAddDTOList()!=null&&addDTO.getTicScenicTypeAddDTOList().size()>0) {
                        //批量插入库存票种配置信息
                        ticScenicTypeMapper.batchInsert(addDTO.getTicScenicTypeAddDTOList(),days.get(i),addDTO.getTicScenicId(),SecurityUtils.getUsername());
                    }
                }
            }
//            //查询详情列表向reids中存放
//            List<TicketDetailVo> pageList = ticScenicStockMapper.selectTickeDetailList();
//            if(pageList!=null&&pageList.size()>0){
//                for (TicketDetailVo vo:pageList) {
//                    //将售卖信息依次放入redis
//                    redisService.setCacheObject("DETAIL:HD_"+vo.getTicketId(),vo);
//                }
//            }
//            //查询日期库存数据放入redis
//            List<TicStockListVo> listRdis = ticScenicStockMapper.selectStockList();
//            if (listRdis!=null&&listRdis.size()>0){
//                for (TicStockListVo vo:listRdis) {
//                    redisService.setCacheObject("STOCK:JQ_"+vo.getScenicId()+"_"+DateUtils.parseDateToStr("yyyy-MM-dd",vo.getScenicStockDate()),vo.getScenicStockCount());
//                }
//            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("新增门票库存异常");
        }
        return 200;
    }

    /**
     * 查询景区门票库存
     *
     * @param queryDTO 景区门票库存主键
     * @return 景区门票库存
     */
    @Override
    public List<TicScenicPageVo> selectTicScenicList(TicScenicQueryDTO queryDTO) {
        return ticScenicStockMapper.selectTicScenicList(queryDTO);
    }

    /**
     * 修改景区门票库存
     *
     * @param updateDTO 景区门票库存
     * @return 结果
     */
    @Override
    public int updateTicScenicStockNew(TicScenicUpdateDTO updateDTO) {
        try {
            TicScenicStock ticScenicStock = ticScenicStockMapper.selectTicScenicStockByScenicStockId(updateDTO.getScenicStockId());
            if(ticScenicStock.getScenicStockDate().before(DateUtils.getNowDate())){
                throw new ServiceException("历史库存不能修改");
            }
            ticScenicStock.setScenicStockCount(updateDTO.getScenicStockCount());
            ticScenicStockMapper.updateTicScenicStock(ticScenicStock);
            if (updateDTO.getTicScenicTypeDetailvoList() != null && updateDTO.getTicScenicTypeDetailvoList().size()>0) {
                for (int i = 0; i < updateDTO.getTicScenicTypeDetailvoList().size(); i++) {
                    TicScenicType ticScenicType = ticScenicTypeMapper.selectTicScenicTypeByTicScenicTypeId(updateDTO.getTicScenicTypeDetailvoList().get(i).getTicScenicTypeId());
                    ticScenicType.setScenicStockPrice(updateDTO.getTicScenicTypeDetailvoList().get(i).getScenicStockPrice());
                    ticScenicTypeMapper.updateTicScenicType(ticScenicType);
                }
            }
            //查询详情列表向reids中存放
            List<TicketDetailVo> pageList = ticScenicStockMapper.selectTickeDetailList();
            if(pageList!=null&&pageList.size()>0){
                for (TicketDetailVo vo:pageList) {
                    //将售卖信息依次放入redis
                    redisService.updateCacheObject("DETAIL:HD_"+vo.getTicketId(),vo);
                }
            }
            //查询日期库存数据放入redis
            List<TicStockListVo> listRdis = ticScenicStockMapper.selectStockList();
            if (listRdis!=null&&listRdis.size()>0){
                for (TicStockListVo vo:listRdis) {
                    redisService.updateCacheObject("STOCK:JQ_"+vo.getScenicId()+"_"+DateUtils.parseDateToStr("yyyy-MM-dd",vo.getScenicStockDate()),vo.getScenicStockCount());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("更新异常");
        }
        return 200;
    }

    /**
     * 查询景区门票库存列表
     *
     * @param
     * @return 景区门票库存集合
     */
    @Override
    public TicScenicDetailVo selectTicScenicStockByScenicStockIdNew(Long scenicStockId) {
        TicScenicDetailVo vo = new TicScenicDetailVo();
        TicScenicVo ticScenicVo = ticScenicMapper.selectTicScenicByScenicIdNew(scenicStockId);
        vo.setTicScenicVo(ticScenicVo);
        TicScenicStockVo stockVo = ticScenicStockMapper.selectTicScenicByScenicIdNew(scenicStockId);
        vo.setTicScenicStockVo(stockVo);
        List<TicScenicTypeDetailVo> list = ticScenicTypeMapper.selectScenicTypeListByScenicId(ticScenicVo.getScenicId(),stockVo.getScenicStockDate());
        vo.setTicScenicTypeDetailvoList(list);
        return vo;
    }

    @Override
    public List<TicType> selectType() {
        TicType ticType = new TicType();
        ticType.setIsDel("0");
        return ticTypeMapper.selectTicTypeList(ticType);
    }
}
